package com.sc.thread.threadcommunication;

/**
 * Created by Shen Chao on 2018-07-03-003.
 * <p>
 * 生产者线程类
 */
public class Producer extends Thread {
    //共享资源缓存类对象
    private ShareCached sCached;

    /**
     * 构造器加入共享资源操作类
     *
     * @param shareCached
     */
    public Producer(ShareCached shareCached) {
        this.sCached = shareCached;
    }

    /**
     * 生产者生产产品，放入共享数据缓存类（仓库）
     * 生产a-d的产品
     */
    @Override
    public void run() {
        for (char p = 'A'; p <= 'D'; p++) {
            try {
                sleep((int) (Math.random() * 3000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //生产产品，放入仓库
            sCached.addShareCachedData(p);
        }
    }
}
